﻿using System;
using System.Collections.Generic;
using System.Web.Mvc;
using WebApplication1.Filters;
using WebApplication1.Models; 
using WebApplication1.ViewModels;



namespace WebApplication1.Controllers
{

    /// <summary>
    /// 利用EF搭建代码有限数据库访问
    /// </summary>
    //[Route("Employee/{action}")]//通过此这操作，将每个操作方法都映射到home的下一个路由
    public class EmployeeController : Controller
    {

        //[RoutePrefix("Employee")]//将整个类映射到Employee/下
        //[Route("{action}")]//添加方法的映射
        //public class EmployeeController : Controller
        //{
        //    //由于路由无法区分employeeId和employeeName的参数类别。所以可以通过内联约束来约束参数类型

        //    [Route("{employeeId:int}")]
        //    public ActionResult Index(int employeeId)
        //    {

        //        return View("Index", employeeListViewModel);//将值返回给View。
        //    }

        //    [Route("{employeeName:string}")]
        //    public ActionResult Index(string employeeName)
        //    {

        //        return View("Index", employeeListViewModel);//将值返回给View。
        //    }

        //}

        // GET: Test6
        [Authorize]//认证属性
        [HeaderFooterFilter]
        public ActionResult Index()
        {
            //建立一个ViewModel存储列表
            EmployeeListViewModel employeeListViewModel = new EmployeeListViewModel
            {
                UserName = User.Identity.Name
            };
            employeeListViewModel.FooterData = new FooterViewModel();
            employeeListViewModel.FooterData.CompanyName = "StepByStepSchools";//Can be set to dynamic value
            employeeListViewModel.FooterData.Year = DateTime.Now.Year.ToString();
            //建立一个BLL层对象
            EmployeeBusinessLayer empBal = new EmployeeBusinessLayer();
            //通过BLL层获取数据  下追BLL层
            List<Employee> employees = empBal.GetEmployees();
            //建立一个视图Model
            List<EmployeeViewModel> empViewModels = new List<EmployeeViewModel>();

         


            //遍历BLL层得到数据，将其值进ViewModel基类对象中
            foreach (Employee emp in employees)
            {
                EmployeeViewModel empViewModel = new EmployeeViewModel();
                empViewModel.EmployeeName = emp.FirstName + " " + emp.LastName;
                empViewModel.Salary = emp.Salary.ToString("C");
                if (emp.Salary > 15000)
                {
                    empViewModel.SalaryColor = "yellow";
                }
                else
                {
                    empViewModel.SalaryColor = "green";
                }
                empViewModels.Add(empViewModel);
            }

            employeeListViewModel.Employees = empViewModels; // 将基类对象传入ViewModel字段中
            return View("Index", employeeListViewModel);//将值返回给View
        }

        //[Route("{year}/{month}/{day}")]//此处通过‘/’作为隔断，捕获三个路径段
        //public ActionResult Dates(string year,string month,string day)
        //{
        //    return View();
        //}

        #region 操作
        //实验9 创建数据入口(Data Entry Screen)   --数据输入界面
        [AdminFilter]//绑定过滤器
        [HeaderFooterFilter]
        public ActionResult AddNew()
        {
            CreateEmployeeViewModel employeeListViewModel = new CreateEmployeeViewModel();
            employeeListViewModel.FooterData = new FooterViewModel();
            //employeeListViewModel.FooterData.CompanyName = "StepByStepSchools";//Can be set to dynamic value
            //employeeListViewModel.FooterData.Year = DateTime.Now.Year.ToString();
            //employeeListViewModel.UserName = User.Identity.Name; //New Line
            return View("CreateEmployee", employeeListViewModel);
        }


        //显示回传数据        
        [AdminFilter]//绑定过滤器
        [HeaderFooterFilter]//绑定过滤器
        public ActionResult SaveEmployee(Employee e, string BtnSubmit)
        {
            switch (BtnSubmit)
            {
                case "Save Employee":
                    if (ModelState.IsValid)
                    {
                        EmployeeBusinessLayer empBal = new EmployeeBusinessLayer();
                        empBal.SaveEmployee(e);
                        return RedirectToAction("Index");
                    }
                    else
                    {
                        CreateEmployeeViewModel vm = new CreateEmployeeViewModel();

                        vm.FooterData = new FooterViewModel();
                        vm.FooterData.CompanyName = "StepByStepSchools";//Can be set to dynamic value
                        vm.FooterData.Year = DateTime.Now.Year.ToString();
                        vm.FirstName = e.FirstName;
                        vm.LastName = e.LastName;
                        if (e.Salary != 0)
                        {
                            vm.Salary = e.Salary.ToString();
                        }
                        else
                        {
                            vm.Salary = ModelState["Salary"].Value.AttemptedValue;
                        }
                        return View("CreateEmployee", vm); // Day 4 Change - Passing e here
                    }
                case "Cancel":
                    return RedirectToAction("Index");
            }
            return new EmptyResult();
        }

        //public ActionResult SaveEmployee()
        //{
        //    Employee e = new Employee();
        //    e.FirstName = Request.Form["FName"];
        //    e.LastName = Request.Form["LName"];
        //    e.Salary = int.Parse(Request.Form["Salary"]);
        //    return null;

        //}
        //public ActionResult SaveEmployee(string FName, string LName, int Salary)
        //{
        //    Employee e = new Employee();
        //    e.FirstName = FName;
        //    e.LastName = LName;
        //    e.Salary = Salary;

        //    return null;
        //}
        #endregion

        public ActionResult GetAddNewLink()
        {
            if (Convert.ToBoolean(Session["IsAdmin"]))
            {
                return PartialView("AddNewLink");
            }
            else
            {
                return new EmptyResult();
            }
        }


    }
}